<template>
  <div>
    <page-list
      :x-uri="uri"
      :doactions="doactions"
      :custom-components="customComponents"
      :checkbox-item="false"
    >
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button
          v-auth="'market.promotion.discount.create'"
          size="mini"
          type="primary"
          @click="create"
        >新建</el-button>
      </template>
    </page-list>
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Tabs from '../tabs';
import Status from '../components/status.vue';
import Course from '../components/course_info.vue';
import Time from '../components/pronmotion_time.vue';
import Price from '../components/price.vue';
import ActiveStatus from '../components/active_status.vue';

export default {
  components: {
    PageList,
    Tabs
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/admin/promotion/discount/list',
      customComponents: {
        status: Status,
        title: Course,
        pronmotion_time: Time,
        price: Price,
        active_status: ActiveStatus
      },
      doactions: [
        {
          title: '活动报表',
          meta: { permission: 'market.promotion.discount.report' },
          callback: this.report
        },
        {
          title: '编辑',
          meta: { permission: 'market.promotion.discount.edit' },
          display({ row }) {
            return row.active_status == 2;
          },
          callback: this.edit
        },
        {
          title: '删除',
          meta: { permission: 'market.promotion.discount.delete' },
          callback: this.delete
        }
      ]
    };
  },
  methods: {
    // 新建
    create: function() {
      this.$router.push({ path: '/promotion/discount/create' });
    },
    // 报表
    report({ row }) {
      this.$router.push({ path: '/promotion/discount/' + row.id + '/report' });
    },
    // 编辑
    edit: function({ row }) {
      this.$router.push({ path: '/promotion/discount/' + row.id + '/edit' });
    },
    // 删除
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/admin/promotion/' + row.id + '/delete',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    }
  }
};
</script>
